package com.offcn.service.impl;

import com.offcn.common.CreateNumberCommon;
import com.offcn.mapper.*;
import com.offcn.pojo.*;
import com.offcn.pojo.vo.ErrorVo;
import com.offcn.pojo.vo.LoginUserVo;

import com.offcn.pojo.vo.MemberVo;
import com.offcn.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    LoginUserMapper loginUserMapper;

    @Autowired
    LoginUserMemberMapper loginUserMemberMapper;

   @Autowired
    RoleMemberMapper roleMemberMapper;

   @Autowired
    RoleMapper roleMapper;

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    MemberClientsMapper memberClientsMapper;


    public  Member selectMemberByLoginUserVo(LoginUserVo loginUserVo) {

//        return memberMapper.selectMemberByLoginVo(loginUserVo);


        LoginUserExample exp = new LoginUserExample();
        LoginUserExample.Criteria criteria = exp.createCriteria();
        criteria.andLoginnameEqualTo(loginUserVo.getUserName());
        criteria.andPasswordEqualTo(loginUserVo.getPassword());
        List<LoginUser> loginUsers = loginUserMapper.selectByExample(exp);
        Integer loginUserId =loginUsers.get(0).getId();


        LoginUserMemberExample exp1 = new LoginUserMemberExample();
        LoginUserMemberExample.Criteria criteria1 = exp1.createCriteria();
        criteria1.andLoginuseridEqualTo(loginUserId);
        List<LoginUserMember> loginUserMembers = loginUserMemberMapper.selectByExample(exp1);
        Integer memberid = loginUserMembers.get(0).getMemberid();


        Member member = memberMapper.selectByPrimaryKey(memberid);
        return  member;
    }

    /*
       根据当前登录人员查询member
       区分是管理员还是部长？？？
        1：根据memberSession查询roleid
        2： if （roleid == 1）经理{ 查询所有}
            if(roleid == 2) 部长{只能查询本部门的}
            if(roleid == 3)员工{您的权限不足}
     */
    @Override
    public List<Member> getMemberBymemberSession(Member memberSession) {


        RoleMemberExample exp1 = new RoleMemberExample();
        RoleMemberExample.Criteria criteria = exp1.createCriteria();
        criteria.andMemberidEqualTo(memberSession.getMid());
        List<RoleMember> roleMembers = roleMemberMapper.selectByExample(exp1);
        Long roleid = roleMembers.get(0).getRoleid();

        if(roleid==1L){

            MemberExample exp2 = new MemberExample();
            List<Member> memberList = memberMapper.selectByExample(exp2);

            return memberList;
        }

        if(roleid==2L){

            MemberExample exp3 = new MemberExample();
            MemberExample.Criteria criteria1 = exp3.createCriteria();
            criteria1.andDepartmentidEqualTo(memberSession.getDepartmentid());
            List<Member> memberList = memberMapper.selectByExample(exp3);
            return  memberList;
        }
        if(roleid==3L){
            List<Member> memberList = new ArrayList<Member>();
            return  memberList;
        }

        return  null;
    }

    @Override
    public List<Member> getMemberBymemberSessionMohu(Member memberSession, MemberVo memberVo) {
        RoleMemberExample exp1 = new RoleMemberExample();
        RoleMemberExample.Criteria criteria = exp1.createCriteria();
        criteria.andMemberidEqualTo(memberSession.getMid());
        List<RoleMember> roleMembers = roleMemberMapper.selectByExample(exp1);
        Long roleid = roleMembers.get(0).getRoleid();

        if(roleid==1L){ //管理员
            List<Member> memberList=  memberMapper.getMemberBymemberSessionMohu(memberVo);
           return memberList;
        }
        if(roleid==2L){


        }
        if(roleid==3L){
            List<Member> memberList = new ArrayList<Member>();
            return  memberList;
        }
        return null;
    }




    @Override
    public boolean save(Member memberSession, Member member, String permissionsId) {

//        member
        int memberId = CreateNumberCommon.getCreateId();
        member.setMid(memberId);
        member.setMcreatetime(new Date());
        int insertMember = memberMapper.insert(member);

//        loginUser
        int loginUserId = CreateNumberCommon.getCreateId();
        LoginUser loginUser = new LoginUser();
        loginUser.setLoginname(member.getMname());
        loginUser.setPassword("666");
        loginUser.setId(loginUserId);
        loginUser.setLogintime(new Date());
        loginUser.setExittime(new Date());
        int insertLoginUser = loginUserMapper.insert(loginUser);

//        loginUserMEMBER
        LoginUserMember loginUserMember = new LoginUserMember();
        loginUserMember.setMemberid(memberId);
        loginUserMember.setLoginuserid(loginUserId);
        int insertLoginUserMember = loginUserMemberMapper.insert(loginUserMember);

//        roleMember
        RoleMember roleMember = new RoleMember();
        roleMember.setMemberid(memberId);
        roleMember.setRoleid(Long.valueOf(permissionsId));
        int insertRoleMember = roleMemberMapper.insert(roleMember);

        if(insertMember == 1 && insertLoginUser == 1 && insertLoginUserMember ==1 && insertRoleMember ==1){
            return true;
        }
        return false;

}

    @Override
    public List<ErrorVo> batchDelete(Member memberSession, ArrayList<Integer> idsList) {

        RoleMemberExample exp1 = new RoleMemberExample();
        RoleMemberExample.Criteria criteria = exp1.createCriteria();
        criteria.andMemberidEqualTo(memberSession.getMid());
        List<RoleMember> roleMembers = roleMemberMapper.selectByExample(exp1);
        Long roleid = roleMembers.get(0).getRoleid();

        ArrayList<ErrorVo> errorVos = new ArrayList<>();

        if(roleid==1L){//经理

            for(Integer memberId :idsList){
//             判断此member是否有客户
                MemberClientsExample exp2 = new MemberClientsExample();
                MemberClientsExample.Criteria criteria2 = exp2.createCriteria();
                criteria2.andMemberidEqualTo(memberId);
                List<MemberClients> memberClients = memberClientsMapper.selectByExample(exp2);
                if(memberClients.size() >0){ //有客户
                    ErrorVo errorVo = new ErrorVo();
                    errorVo.setName(memberMapper.selectByPrimaryKey(memberId).getMname());
                    errorVo.setError("此员工下有客户，不能删除！！！");
                    errorVos.add(errorVo);
                }else{
//                删除4张表！！
                    System.out.println("进行删除------删除4张表");
                }

            }
            return errorVos;
        }


        if(roleid==2L){



        }



        return null;
    }


    @Override
    public Role selectRoleBySessionMember(Member sessionMember) {


       /* RoleMemberExample exp= new RoleMemberExample();
        RoleMemberExample.Criteria criteria = exp.createCriteria();
        criteria.andMemberidEqualTo(sessionMember.getMid());
        List<RoleMember> roleMembers = roleMemberMapper.selectByExample(exp);
        System.out.println("========================");
        System.out.println("roleMembers"+roleMembers.get(0));
        Long roleid = roleMembers.get(0).getRoleid();
        Role role = roleMapper.selectByPrimaryKey(roleid);
        System.out.println(role.getId());
        System.out.println(role.getName());
        System.out.println(role.getRedescribe());
        return role;*/
       return roleMapper.selectRoleBySessionMember(sessionMember);
    }


}
